In [6]:
file = '/Users/schriste/Desktop/FOXSI-SAAS-Mock-Target-Test.png'

In [12]:
import skimage
from skimage.io import imread
from skimage import data, filter, color
from skimage.transform import hough_circle
from skimage.feature import peak_local_max
from skimage.draw import circle_perimeter
from skimage.util import img_as_ubyte
print('skimage version %s' % skimage.__version__)


skimage version 0.10.0

In [133]:
import matplotlib.pyplot as plt
%matplotlib inline
import matplotlib as mpl
mpl.rcParams['figure.figsize'] = (5, 5)
import matplotlib.cm as cm

In [24]:
import numpy as np

In [13]:
image = imread(file)

In [14]:
image


Out[14]:
array([[255, 255, 255, ..., 255, 255, 255],
       [255, 255, 255, ..., 255, 255, 255],
       [255, 255, 255, ..., 255, 255, 255],
       ..., 
       [255, 255, 255, ..., 255, 255, 255],
       [255, 255, 255, ..., 255, 255, 255],
       [255, 255, 255, ..., 255, 255, 255]], dtype=uint8)

In [134]:
plt.imshow(image, cmap=cm.Greys)


Out[134]:
<matplotlib.image.AxesImage at 0x10ff320d0>

In [135]:
edges = filter.canny(image, sigma=1)

In [136]:
plt.imshow(edges, cmap=cm.Greys)


Out[136]:
<matplotlib.image.AxesImage at 0x10a414a90>

In [164]:
hough_radii = np.arange(15, 300, 5)
hough_res = hough_circle(edges, hough_radii)

In [165]:
hough_radii


Out[165]:
array([ 15,  20,  25,  30,  35,  40,  45,  50,  55,  60,  65,  70,  75,
        80,  85,  90,  95, 100, 105, 110, 115, 120, 125, 130, 135, 140,
       145, 150, 155, 160, 165, 170, 175, 180, 185, 190, 195, 200, 205,
       210, 215, 220, 225, 230, 235, 240, 245, 250, 255, 260, 265, 270,
       275, 280, 285, 290, 295])

In [166]:
centers = []
accums = []
radii = []

for radius, h in zip(hough_radii, hough_res):
    # For each radius, extract two circles
    peaks = peak_local_max(h, num_peaks=2)
    centers.extend(peaks)
    accums.extend(h[peaks[:, 0], peaks[:, 1]])
    radii.extend([radius, radius])

Now filter the results


In [171]:
best_centers = []
best_radii = []
best_x = []
best_y = []
number_of_best_circles = 10
for idx in np.argsort(accums)[::-1][:number_of_best_circles]:
    center_x, center_y = centers[idx]
    best_x.append(center_x)
    best_y.append(center_y)
    best_centers.append(centers[idx])
    best_radii.append(radii[idx])

In [172]:
fig, ax = plt.subplots()
plt.imshow(image, cmap=plt.cm.gray)
for center, radius in zip(best_centers, best_radii):
    circle = plt.Circle((center[1], center[0]), radius, color='r', fill=False)
    ax.add_patch(circle)
plt.show()



In [173]:
print("Number of circles found: %s" % len(best_y))
print("Radii found: %s" % best_radii)


Number of circles found: 10
Radii found: [170, 170, 85, 85, 115, 115, 200, 55, 55, 140]

Center with error


In [174]:
print("Calibrated Center X = %s +/- %s" % (np.average(best_x), np.std(best_x)))
print("Calibrated Center Y = %s +/- %s" % (np.average(best_y), np.std(best_y)))


Calibrated Center X = 395.9 +/- 0.830662386292
Calibrated Center Y = 305.5 +/- 0.67082039325

In [ ]: